using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroEquipCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroEquipCell
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject itemGo { protected set; get; }
            public UnityEngine.GameObject lockTip { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLock { protected set; get; }
            public UnityEngine.GameObject imageAdd { protected set; get; }
            public UnityEngine.GameObject selectRoot { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("itemGo", out var __tbv0);
                this.itemGo = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockTip", out var __tbv1);
                this.lockTip = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLock", out var __tbv2);
                this.textLock = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("imageAdd", out var __tbv3);
                this.imageAdd = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("selectRoot", out var __tbv4);
                this.selectRoot = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private UIN_OneItem item;

        #endregion fields

        #region properties

        protected UIB_UIN_HeroEquipCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroEquipCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.item = this.InitChildNode<UIN_OneItem>(this.ui.itemGo);
            this.SetSelected(false);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.item = null;
        }

        #region methonds

        public void Show(HeroData heroData, int index)
        {
            this.Show();
            this.ShowLock(heroData.level, index);

            int column = index + 1;
            ItemEquipmentData equipItem = heroData.GetWearEquipItem(column) as ItemEquipmentData;
            this.ShowEquipItem(equipItem);
            this.ShowAdd(equipItem == null);
        }

        private void ShowLock(int heroLv, int index)
        {
            int limitLv = APIUIHeroInfo.LimitLvUnlockHeroEquipColumns[index];
            bool isUnlock = limitLv <= heroLv;

            bool isActiveLock = !isUnlock;
            this.ui.lockTip.SetActive(isActiveLock);
            if (isActiveLock)
            {
                this.ui.textLock.text = APIUIHeroInfo.GetHeroEquipColumnUnlockStr(limitLv);
            }
        }

        private void ShowEquipItem(ItemEquipmentData equipItem)
        {
            bool isHasEquip = equipItem != null;
            if (isHasEquip)
            {
                this.item.ShowEquip(equipItem);
            }
            else
            {
                this.item.Hide();
            }
        }

        private void ShowAdd(bool isActive)
        {
            this.ui.imageAdd.SetActive(isActive);
        }

        public void SetSelected(bool isSelected)
        {
            this.ui.selectRoot.SetActive(isSelected);
        }
        #endregion methonds
    }
}
